<?php

namespace app\model;

use app\traits\ModelQueryTrait;
use think\facade\Db;
use think\Model;
use think\model\relation\BelongsTo;

class ThirdVideos extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'third_videos';

    // 设置当前模型主键
    protected $pk = 'id';

    protected $autoWriteTimestamp = true;

    /**
     * 创建时间字段 false表示关闭
     * @var false|string
     */
    protected $createTime = 'created_at';

    /**
     * 更新时间字段 false表示关闭
     * @var false|string
     */
    protected $updateTime = 'updated_at';

    use ModelQueryTrait;

    protected $json = ['config','applet_ids'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    public function video(): BelongsTo
    {
        return $this->belongsTo('app\model\VideoDetails','video_detail_id');
    }

    /**
     * 获取抖音媒资aid,eid
     * @param $videoIds
     * @param $detailIds
     * @return array
     */
    public static function getDyMediaParam($videoIds, $detailIds): array
    {
        $videoIds = array_unique($videoIds);
        $originVideos = Videos::where('is_copy',1)
            ->whereIn('id', $videoIds)
            ->whereNotNull('original_id')
            ->column('original_id','id');

        $copyIds = [];
        if ($originVideos) {
            $originIds = array_values($originVideos);
            $newVideoIds = array_unique(array_merge($videoIds, $originIds));
            $copyIds = array_keys($originVideos);
        } else {
            $newVideoIds = $videoIds;
        }

        $albumIds = self::whereIn('video_id', $newVideoIds)
            ->where('platform',2)
            ->column('album_id','video_id');

        foreach ($copyIds as $copyId) {
            if (isset($albumIds[$originVideos[$copyId]])) {
                $albumIds[$copyId] = $albumIds[$originVideos[$copyId]];
            }
        }

        $details = VideoDetails::field('id,video_id,sort,platform_config')->whereIn('id', $detailIds)->select()->toArray();

        $episodeIds = [];
        $originDetails = [];
        foreach ($details as $detail) {
            if (in_array($detail['video_id'], $copyIds)) {
                $kk = $originVideos[$detail['video_id']].'-'.$detail['sort'];
                if (!isset($originDetails[$kk])) {
                    $newDetail = VideoDetails::field('id,platform_config')
                        ->where('video_id', $originVideos[$detail['video_id']])
                        ->where('sort', $detail['sort'])
                        ->find();
                    if ($newDetail) {
                        $originDetails[$kk] = $newDetail->toArray();
                    }
                }
                $platformConf = isset($originDetails[$kk]) ? ($originDetails[$kk]['platform_config'] ?: []) : [];
            } else {
                $platformConf = $detail['platform_config'] ?: [];
            }
            $platformConf2 = $platformConf['douyinyun'] ?? [];
            $episode_id = $platformConf2['episode_id'] ?? '';
            if ($episode_id) {
                $episodeIds[$detail['id']] = $episode_id;
            }
        }

        return [$albumIds, $episodeIds];
    }
}